//
//  TitleButton.swift
//  jackWu
//
//  Created by Lewhaming on 2016/11/23.
//  Copyright © 2016年 IQ. All rights reserved.
//

import UIKit

class TitleButton : UIButton{

    override init(frame: CGRect) {
        super.init(frame:frame)
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: UIControlState.normal)
        setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        sizeToFit()
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: UIControlState.selected)
       
    }
    // 通过xib
    required init?(coder aDecoder: NSCoder) {
        //系统默认initWithcoder
        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    private func setupUI(){
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: UIControlState.normal)
        setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        sizeToFit()
        setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: UIControlState.selected)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        titleLabel?.frame.offsetBy(dx: -(imageView?.frame.width)!, dy: 0)
//        imageView?.frame.offsetBy(dx: (titleLabel?.frame.width)!, dy: 0)
        
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.width
    }
    
    override func setTitle(_ title: String?, for state: UIControlState) {
        super.setTitle((title ?? "") + "  ", for: state)
    }
}
